package com.fengmi.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.fengmi.dto.PageDTO;
import com.fengmi.entity.MallGoodsBrand;
import com.fengmi.mapper.MallGoodsBrandMapper;
import com.fengmi.service.IMallGoodsBrandService;
import com.fengmi.vo.PageResultVO;
import com.fengmi.vo.ResultVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 作者：陈学虎
 * 时间：2021/7/6 0006 下午 5:26
 */
@Service
//事务
@Transactional
public class MallGoodsBrandService implements IMallGoodsBrandService {

//    @Autowired
    private MallGoodsBrandMapper mallGoodsBrandMapper;

    public MallGoodsBrandService(MallGoodsBrandMapper mallGoodsBrandMapper) {
        this.mallGoodsBrandMapper = mallGoodsBrandMapper;
    }

    @Override
    public PageResultVO<MallGoodsBrand> findBrandByWhere(PageDTO<MallGoodsBrand> pageDTO) {
        if (pageDTO == null){
            return new PageResultVO<>(false,"请求参数错误");
        }

        PageHelper.startPage(pageDTO.getPage(),pageDTO.getSize());

        List<MallGoodsBrand> brands = mallGoodsBrandMapper.findBrandByWhere(pageDTO.getWhere());

        PageInfo<MallGoodsBrand> pageInfo = new PageInfo<>(brands);

        PageResultVO<MallGoodsBrand> pageResultVO = new PageResultVO<>(true,"success");

        pageResultVO.setData(pageInfo.getList());
        pageResultVO.setPages(pageInfo.getPages());
        pageResultVO.setTotal(pageInfo.getTotal());

        return pageResultVO;
    }

    @Override
    public ResultVO delById(Integer id) {

        if (id == null){
            return new ResultVO(false,"请求参数错误");
        }

        int i = mallGoodsBrandMapper.delById(id);

        return new ResultVO(i==1?true:false,i==1?"成功":"失败");
    }

    @Override
    public ResultVO addBrand(MallGoodsBrand mallGoodsBrand) {
        if (ObjectUtil.isEmpty(mallGoodsBrand)){
            return new ResultVO(false,"请求参数错误");
        }

        int i = mallGoodsBrandMapper.addBrand(mallGoodsBrand);

        return new ResultVO(i==1?true:false,i==1?"成功":"失败");
    }

    @Override
    public ResultVO updateBrand(MallGoodsBrand mallGoodsBrand) {
        if (ObjectUtil.isEmpty(mallGoodsBrand)){
            return new ResultVO(false,"请求参数错误");
        }

        int i = mallGoodsBrandMapper.updateBrand(mallGoodsBrand);

        return new ResultVO(i==1?true:false,i==1?"成功":"失败");
    }

    @Override
    public ResultVO delBactch(List<MallGoodsBrand> mallGoodsBrandList) {
        if (ObjectUtil.isEmpty(mallGoodsBrandList)){
            return new ResultVO(false,"请求参数错误");
        }

        int i = mallGoodsBrandMapper.delBactch(mallGoodsBrandList);
        return new ResultVO(i>=1?true:false,i>=1?"成功":"失败");
    }

    @Override
    public ResultVO findOne(Integer id) {
        MallGoodsBrand goodsBrand = mallGoodsBrandMapper.findOne(id);
        if (ObjectUtil.isEmpty(goodsBrand)){
            return new ResultVO(false,"查询失败");
        }
        return new ResultVO(true,"查询成功",goodsBrand);
    }

    @Override
    public ResultVO findAll() {

        List<MallGoodsBrand> brands = mallGoodsBrandMapper.findBrand();
        return new ResultVO(true,"查询成功",brands);
    }
}
